<?php

class ExpedienteController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
      /* Initialize action controller here */
         $this->_helper->layout->setLayout('layout-expediente');

    }

    public function indexAction()
    {
        // action body
        try{
          if ($this->getRequest()->isPost()){
            if(!empty($_POST['placa'])){
              $placa = $_POST['placa'];
              $vehicleDB = new Application_Model_DbTable_Vehicle();
              $vehicle = $vehicleDB->getVehicle($placa);
              if(!empty($vehicle)){
                $this->view->vehicle = $vehicle;
                
                //$this->_helper->redirector('add');
              }
            }
          }
        }
        catch(Exception $e){
            throw $e;
        }
    }

    public function findAction()
    {
        $this->_helper->layout->setLayout('layout-home');
        $customers = new Application_Model_DbTable_Record();
        $this->view->expedientes = $customers->findRecordProforma();
        $this->view->menuIndex = 'Home';

    }

    public function addAction()
    {
        // action body
        $fecha = date("Y-m-d"); 

        if ($this->getRequest()->isPost()) 
        {
            if(!empty($_POST['placa'])){
            	$placa = $_POST['placa'];
            	$type = $_POST['type'];

                $observation = $_POST['observacion'];
                $count = $_POST['count'];
                $pieceDescription = $_POST['piecedescription'];
                $piecePrice = $_POST['pieceprice']; 
                $descriptionService = $_POST['piecedescriptionService'];
                $priceService = $_POST['piecepriceService']; 
               
               $TotalRepuesto = 0;
               $customers = new Application_Model_DbTable_DetailRecord();
               foreach(array_keys($count) as $i) { 
                	# code...
               		$countRepuestos = $count[$i];
               		$description = $pieceDescription[$i];
               		$price = $piecePrice[$i];
               		$Total = $countRepuestos * $price ;

                  $TotalRepuesto += $Total;
                  $customers->addDetailRecord($placa, $fecha,$countRepuestos, $description, $price,'R');//R = Respuesto
                }

                  
                $totalService = 0;
                foreach(array_keys($descriptionService) as $i) { 
                	# code...
               		
               		$description = $descriptionService[$i];
               		$price = $priceService[$i];
                  
                  $totalService += $price;
                  $customers->addDetailRecord($placa, $fecha, 1 , $description, $price,'S');//S = Servicio
                }
                  
                $records = new Application_Model_DbTable_Record();
                $records->addRecord($placa, $fecha,$observation,$type,$totalService,$TotalRepuesto);//P = Proforma

              }  


        }

    }

    public function editAction()
    {
        // action body
       
      if(!empty($_POST['nombre'])){   
            
          $ced = $_POST['cedula'];
          $name = $_POST['nombre'];
          $fecha = $_POST['date'];
          $phone = $_POST['telefono']; 
          $email = $_POST['correo'];               
          try{
              $customers = new Application_Model_DbTable_Client();
              $customers->updateClient($id,$ced,$name,$fecha, $phone, $email);
              
              $this->_helper->redirector('index');
          }
          catch(Exception $e){
              throw $e;
          }
      }

    }


}



